/**
 * Workspace Constructor
 * @class Provides methods to interact with an existing workspace.
 * @param {String} URL The web address that points to the designated workspace.
 * @property {String} URL The web address that points to the designated Team Site.
 * @property {String[]} Files A collection of Document library filenames.
 * @requires SharePoint
 */
function Workspace (URL, ForceCreate) {

	var self = this;
	
	this.URL = URL;
	
	/**
	 * Move a single file from this workspace to another
	 * @param {String} Filename The filename of the file that should be moved
	 * @param {Workspace|Workspace[]} Destination The worspace the file should be moved to. Can be an array of multiple workspaces.
	 */
	this.Move = function (Filename, Destination) {
		$().SPServices({
			operation: "CopyIntoItemsLocal",
			SourceURL: self.URL + "/" + Filename,
			DestinationUrls: [Destination.URL + "/" + Filename],
			completefunc: function (xData, Status) {
				console.log(xData.responseText);
			}
		});
	}
	
	/**
	 * Create a new instance of the Team Site this workspace belongs to.
	 * @returns TeamSite
	 */
	this.GetTeamSite = function () {
		return new TeamSite ();
	 }
	 
	 /**
	  * Fetch most recent data relating to the workspace from the server.
	  * @returns Boolean True on success, false upon failure.
	  */
	this.Refresh = function () {
		
		var success = true;
		
		// Get the properties of the workspace
		$().SPServices ({
			operation: "GetWeb",
			webURL: self.URL,
			async: false,
			completefunc: function (result, status) {
				if (status == "success") {
					var properties = $(status).find("Web");
					self.Title = properties.attr ("Title");
					self.Description = properties.attr ("Description");
					self.Language = properties.attr ("Language");
					self.Theme = properties.attr ("Theme");
				} else {
					if (ForceCreate) {
						self.Push ();
					} else {
						success = false;
					}
				}
			}
		});
		
		// Fetch the files of the document library
		self.Documents = [];
		$().SPServices ({
			operation: "GetListItems",
			webURL: self.URL,
			listName: "Document Library",
			async: false,
			completefunc: function (result, status) {
				if (status == "success") {
					$(result.responseXML).find ("rs\\:data > z\\:row").each (function () {
						var row = $(this);
						self.Documents[row.attr("ows_FileRef")] = row.attr ("ows_LinkFilename");
					});
				} else {
					success = false;
				}
			}
		});
		
		// Fetch the files of the mail libary
		self.Mails = [];
		// TODO
		
		return success;
	 }
	 
	/**
	 * Push the modifications to the changes of the workspace to the web.
	 */
	this.Push = function () {
		
	}
	 
	// Initialize workspace
	this.Refresh ();
}

/**
 * This object might provide functionality to manipulate files in the future.
 * @class Holds a list of all files that are instanciated.
 * @param {String} URL The full web address to the file.
 */
function File (URL) {
	
	this.Refresh () = function () {
		// TODO
	}
	
	// Initialize file
	this.Refresh ();
	
}